package config

import (
	"github.com/joho/godotenv"
	"os"
)

var (
	App      *app
	Http     *http
	Redis    *redis
	DB       *db
	Session  *session
	Services *services
)

func init() {

	err := godotenv.Load(".env")
	if err != nil {
		panic(err.Error())
	}

	appDebug := false
	if env("APP_DEBUG", "false") == "true" {
		appDebug = true
	}
	App = &app{
		Name:  env("APP_NAME", "APP NAME"),
		Env:   env("APP_ENV", "local"),
		Debug: appDebug,
	}

	DB = &db{
		DSN: env("DB_DSN", "127.0.0.1:3306"),
	}

	Http = &http{
		Listen: env("HTTP_LISTEN", "127.0.0.1:8199"),
	}

	Redis = &redis{
		Host:     env("REDIS_HOST", "127.0.0.1:6379"),
		Password: env("REDIS_PASSWORD", ""),
	}

	Session = &session{}

	Services = &services{
		TraceEndpoint: env("SERVICE_TRACE_ENDPOINT", ""),
	}
}

func env(name string, def string) string {
	value := os.Getenv(name)
	if len(value) > 0 {
		return value
	}
	return def
}
